import Base from "../Base.js";
import theatersApi from "../../api/theaters.js";
import adminsApi from "../../api/admins.js";
import seatsApi from '../../api/seats.js'
export default class extends Base {
  constructor(props) {
    super(props);
    this.data = {}
    this._id = props._id;
    this.temp = '';
  }
  handle() {
    layui.table.on('tool(theaters_search)', async (obj) => {
      //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
      // var data = obj.data; //获得当前行数据
      var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
      // var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
      const _id = obj.data._id;
      if (layEvent == "update") {
        let data = await theatersApi.getOne(_id);
        console.log(data);
        sessionStorage.setItem('data', JSON.stringify(data.data))
        location.hash = '#/info/theaters-upDate'
      } else if (layEvent == "delete") {
        let state = confirm('确认删除？');
        if (state) {
          let data = await theatersApi.delete({ _id });
          let data1 = await seatsApi.delete(_id);
          this.render();
        }
      } else if (layEvent == "seat") {
        location.hash = `#info/updateSeat/${_id}`
      }
    })
  }
  render() {
    this.el.html(`
    <table id="theaters_search" lay-filter="theaters_search"></table>
    <script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="update">更新</a>
    <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="seat">座位管理</a>
</script>
    `);
    this.reRender();
  }
  reRender() {
    layui.table.render({
      elem: '#theaters_search'
      , method: "get"
      , url: `/api/theaters/search/${this._id}` //数据接口
      , headers: { Authorization: `Bearer ${adminsApi.getToken()}` }
      , page: true //开启分页
      , limits: [5, 10, 15, 20]
      , parseData: function (res) {
        let data = res;
       
      },
      cols: [[ //表头
        { field: '_id', title: 'ID', sort: true, fixed: 'left' }
        , { field: 'name', title: '名称' }
        , { field: 'cinemasId', title: '所属影院', sort: true, templet: '<div>{{d.cinemasId.name}}</div>' }
        , { field: 'status', title: '是否营业', sort: true }
        , { fixed: 'right', align: 'center', toolbar: '#barDemo' }
      ]],
    });
  }
}
